/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "KeyEventHandler.h"
#include "KeyMapper.h"

#include <input/MediatorTypes.h>
#include <input/OIS/Manager.h>
#include <input/OIS/Producer.h>

#include <mediator/InterMediator.h>

namespace galaxy {
    namespace input {
        namespace ois_input {

            KeyEventHandler::KeyEventHandler(boost::shared_ptr<Manager> manager)
            {
                m_Manager = manager;
            }

            KeyEventHandler::~KeyEventHandler(void)
            {
                m_Manager.reset();
            }

            bool KeyEventHandler::keyPressed( OIS::KeyEvent const &e ) {
                input::KeyEvent event(mediator::CATEGORY_INPUT, 
                    MODULE_TYPE_INPUT_KEY, MODULE_SUBTYPE_INPUT_KEYPRESS);

                event.m_Key = e.key;
                event.m_Modifiers = getModifiers ();
                event.m_Text = e.text;

                boost::shared_ptr<Manager> manager = m_Manager.lock();
                if (manager) {
                    event.m_KeyAction = manager->m_KeyMap->getAction(e.key, event.m_Modifiers);
                }

                mediator::InterMediator::postEvent(event, mediator::POST_IMMEDIATE);

                return true;
            }

            bool KeyEventHandler::keyReleased( OIS::KeyEvent const &e ) {
                input::KeyEvent event(mediator::CATEGORY_INPUT, 
                    MODULE_TYPE_INPUT_KEY, MODULE_SUBTYPE_INPUT_KEYRELEASE);

                event.m_Key = e.key;
                event.m_Modifiers = getModifiers ();
                event.m_Text = e.text;

                boost::shared_ptr<Manager> manager = m_Manager.lock();
                if (manager) {
                    event.m_KeyAction = manager->m_KeyMap->getAction(e.key, event.m_Modifiers);
                }

                mediator::InterMediator::postEvent(event, mediator::POST_IMMEDIATE);

                return true;
            }

            unsigned int KeyEventHandler::getModifiers () {
                int modifiers = 0;
                boost::shared_ptr<Manager> manager = m_Manager.lock();
                if (manager->m_Keyboard->isModifierDown (OIS::Keyboard::Alt)) {
                    modifiers |= OIS::Keyboard::Alt;
                }
                if (manager->m_Keyboard->isModifierDown (OIS::Keyboard::Ctrl)) {
                    modifiers |= OIS::Keyboard::Ctrl;
                }
                if (manager->m_Keyboard->isModifierDown (OIS::Keyboard::Shift)) {
                    modifiers |= OIS::Keyboard::Shift;
                }
                return modifiers;
            }

        } // namespace ois_input
    } // namespace input
} // namespace galaxy
